/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.nanhill.xmlro.sro.spring;

import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.nanhill.commons.lang.ReflectCache;
import org.nanhill.xmlro.sro.SroHelper;

/**
 * This is a wrapper used for exporting xmlro service . 
 * @author alin
 */
public class SroSimpleControllerExporter extends SroAbstractControllerExporter {
	private Object service;

	public void setService(Object service) {
		this.service = service;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.xmlro.spring.AbstractXmlroControllerExporter#process(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected Method getCallMethod(HttpServletRequest request, Object service)
			throws Exception {
		// get method name
		String methodName = request.getParameter(SroHelper.SRO_METHOD);
		if (null == methodName) {
			throw new ServletException(
					"Http request must contains a parameter ["
							+ SroHelper.SRO_METHOD + "] to call server method!");
		}
		// {{get RPC method
		Class serviceClass = service.getClass();
		// invoke the first public method by the input method name
		Method method = ReflectCache.getFirstPublicMethod(serviceClass,
				methodName);
		// }}
		return method;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.nanhill.xmlro.spring.AbstractXmlroControllerExporter#getCallService(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object getCallService(HttpServletRequest request)
			throws Exception {
		Object service = this.service;
		return service;
	}

}
